package cn.itcast.controlpanel;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        generate_signature();
    }

    private void generate_signature() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        String gmt = dateFormat.format(new Date()) + "Z";


        Map<String, String> map = new HashMap<String, String>();
        // 公共参数
        map.put("Format", "JSON");
        map.put("Version", "2018-01-20");
        map.put("AccessKeyId", Config.accessKey);
        map.put("SignatureMethod", "HMAC-SHA1");
        map.put("Timestamp", gmt);
        map.put("SignatureVersion", "1.0");
        map.put("SignatureNonce", "15215528852330");
        map.put("RegionId", "cn-shanghai");
        // 请求参数
        map.put("Action", "SetDeviceProperty");
        map.put("DeviceName", "light1");
        map.put("ProductKey", "hgm10kidTFC");
        map.put("IotInstanceId", "iot-06z00f3ydsep6u4");
        map.put("Items", "{\"LightSwitch\":0}");
        try {
            String signature = SignatureUtils.generate("GET", map, Config.accessKeySecret);
            System.out.println("最终signature: " + signature);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//GET&%2F&
// AccessKeyId%3DLTAI5tAkFZoCwBhMCvByG2em%26
// Action%3DSetDeviceProperty%26
// DeviceName%3Dlight1
// %26Format%3DJSON
// %26Items%3D%257B%2522LightSwitch%2522%253A1%252C%2522LightCurrent%2522%253A%25221.2%2522%257D
// %26ProductKey%3Dhgm10kidTFC
// %26RegionId%3Dcn-shanghai
// %26SignatureMethod%3DHMAC-SHA1
// %26SignatureNonce%3D15215528852300
// %26SignatureVersion%3D1.0
// %26Timestamp%3D2022-07-12T03%253A39%253A53Z
// %26Version%3D2018-01-20"